﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Windows.Forms;
using HFK.DxFluentUI.Utility;
using HFK.DxFluentUI.LanguageModel;
using HFK.DxFluentUI.LanguageModel.HorizontalGrid;
using HFK.DxFluentUI.HorizontalGrid.ComponentModel;

namespace HFK.DxFluentUI.HorizontalGrid.Columns
{
  public class PropertyColumnContext<T> : HorizontalGridColumnElement<T> 
  {
    #region public members

    public ContinuationAndElement<HorizontalGridColumnElement<T>> BindToProperty(string propertyName)
    {
      m_control.FieldName = propertyName;

      return new ContinuationAndContext<HorizontalGridColumnElement<T>>(this);
    }

    public ContinuationAndElement<HorizontalGridColumnElement<T>> BindToProperty<U>(Expression<Func<T, U>> expression)
    {
      m_control.FieldName = DevExpressPropertyDescriptorSyntax.PropertyReference<T, U>(expression); // ChildPropertySyntax.Create<T, U>(expression).PropertyName;

      return new ContinuationAndContext<HorizontalGridColumnElement<T>>(this);
    }

    public FeatureActivationWithConfigurationElement<EditableValuesElement<ContinuationAndElement<HorizontalGridColumnElement<T>>>, ContinuationAndElement<HorizontalGridColumnElement<T>>> WithEditing()
    {
      return new FeatureWithConfigurationContext<EditableValuesElement<ContinuationAndElement<HorizontalGridColumnElement<T>>>, ContinuationAndElement<HorizontalGridColumnElement<T>>>(
        this.WithEditingAllowed,
        this.WithEditingAllowedUsing,
        this.WithEditingNotAllowed);
    }

    public FeatureActivationElement<ContinuationAndElement<HorizontalGridColumnElement<T>>> WithSorting()
    {
      return new FeatureContext<ContinuationAndElement<HorizontalGridColumnElement<T>>>(this.WithSortingAllowed, this.WithSortingNotAllowed);
    }

    public FeatureActivationWithConfigurationElement<FilterSelectorElement<ContinuationAndElement<HorizontalGridColumnElement<T>>>, ContinuationAndElement<HorizontalGridColumnElement<T>>> WithFiltering()
    {
      return new FeatureWithConfigurationContext<FilterSelectorElement<ContinuationAndElement<HorizontalGridColumnElement<T>>>, ContinuationAndElement<HorizontalGridColumnElement<T>>>(
        this.WithFilteringAllowed,
        null, //this.WithFilteringAllowedUsing,
        this.WithFilteringNotAllowed);
    }

    #endregion

    #region internal members

    internal DevExpress.XtraGrid.Columns.GridColumn ImplementingControl { get { return m_control; } }
    internal HorizontalGridControl GridControl { get { return m_gridFactory.ImplementingControl; } }

    internal static PropertyColumnContext<T> Create(HorizontalGridControlBase gridFactory, string caption)
    {
      return new PropertyColumnContext<T>(gridFactory, caption);
    }

    public ContinuationAndElement<HorizontalGridColumnElement<T>> WithEditingAllowed()
    {
      m_control.OptionsColumn.AllowEdit = true;
      return new ContinuationAndContext<HorizontalGridColumnElement<T>>(this);
    }

    public EditableValuesElement<ContinuationAndElement<HorizontalGridColumnElement<T>>> WithEditingAllowedUsing()
    {
      m_control.OptionsColumn.AllowEdit = true;
      return new PropertyColumnEditableValuesContext<T>(this);
    }

    public ContinuationAndElement<HorizontalGridColumnElement<T>> WithEditingNotAllowed()
    {
      m_control.OptionsColumn.AllowEdit = false;
      return new ContinuationAndContext<HorizontalGridColumnElement<T>>(this);
    }

    public ContinuationAndElement<HorizontalGridColumnElement<T>> WithSortingAllowed()
    {
      m_control.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.True;
      return new ContinuationAndContext<HorizontalGridColumnElement<T>>(this);
    }

    public ContinuationAndElement<HorizontalGridColumnElement<T>> WithSortingNotAllowed()
    {
      m_control.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;
      return new ContinuationAndContext<HorizontalGridColumnElement<T>>(this);
    }

    public ContinuationAndElement<HorizontalGridColumnElement<T>> WithFilteringAllowed()
    {
      m_control.OptionsFilter.AllowFilter = true;
      return new ContinuationAndContext<HorizontalGridColumnElement<T>>(this);
    }

    public ContinuationAndElement<HorizontalGridColumnElement<T>> WithFilteringNotAllowed()
    {
      m_control.OptionsFilter.AllowFilter = false;
      return new ContinuationAndContext<HorizontalGridColumnElement<T>>(this);
    }

    #endregion

    #region private members

    DevExpress.XtraGrid.Columns.GridColumn m_control = new DevExpress.XtraGrid.Columns.GridColumn();
    HorizontalGridControlBase m_gridFactory;

    private PropertyColumnContext(HorizontalGridControlBase gridFactory, string caption)
    {
      m_gridFactory = gridFactory;

      m_control.Name = string.Format("{0}.[{1}]", caption, System.Guid.NewGuid().ToString());
      m_control.Caption = caption;
      m_control.Visible = true;
      m_control.VisibleIndex = 0;
    }
    
    #endregion
  }
}
